import fetch from 'isomorphic-fetch'
import { getCookie,downloadFile } from '../config/lib'
import * as name from '../config/constant'

export const disk_init = (dispatch,token) => {
    if(token === null){
      return dispatch(init_fail())
    }
    return (dispatch) => {fetch(name._API+'/disk/init?token='+token,{
        method: 'GET',
        mode: 'cors',
        headers: {
            'Accept': 'application/json',
        }
    })
    .then(response => response.json())
    .then(function theEnd(response){
        dispatch(init_success(response))
    })
    }
}

export const init_success = (response) => {
    return {
        type: name.DISK_INIT,
        quota: response.quota,
        used: response.used,
        rootDir: response.rootDir,
        Dirs: response.dirs,
        DirStack: response.rootDir,
        Node: response.list
    }
}

export const init_fail = () => {
  return {
    type: name.INIT_FAIL
  }
}

export const cd_dir = (dispatch,parent) => {
  let token = getCookie('token')
  let request = {
    "parent":parent
  }
  return (dispatch) => {fetch(name._API+'/disk?action=list&token='+token,{
      method: 'POST',
      mode: 'cors',
      body:JSON.stringify(request)
  })
  .then(response => response.json())
  .then(function success(response){
    if (response.code === 200) {
      dispatch(cd_dir_success(response))
    }
  })
  }
}

export const cd_dir_success = (response) => {
  return {
    type: name.CD_DIR_SUCCESS,
    Node: response.list,
    DIR: {
      "directory":response.list.directory,
      "tag":response.list.tag
    }
  }
}

export const return_dir = (dispatch,DirStack,directory) => {
  let count = (DirStack.length === 1)?-1:DirStack.length-1
  while(count--){
    DirStack.pop()
    if (DirStack[count].directory === directory) {
      DirStack.pop()
      break
    }
  }
  dispatch(cd_dir(dispatch,directory))
  return {
    type: name.RETURN_DIR,
    _DirStack: DirStack
  }
}

export const new_dir = (dispatch,parent,dir_name) => {
  let token = getCookie('token')
  let request = {
    "parent":parent,
    "dir_name":dir_name
  }
  return (dispatch) => {fetch(name._API+'/disk?action=mkdir&token='+token,{
      method:'POST',
      mode: 'cors',
      body: JSON.stringify(request)
  })
  .then(response => response.json())
  .then(function success(response){
    if (response.code === 200 ) dispatch(new_dir_success(response))
    else if(response.code === 403) dispatch(new_dir_fail())
  })
  }
}

export const new_dir_success = response => {
  return {
    type: name.NEWDIR_SUCCESS,
    Dirs: response.dirs,
    Node: response.list
  }
}

export const new_dir_fail = () => {
  return {
    type: name.NEWDIR_FAIL
  }
}

export const delete_node = (dispatch,parent,deletelist) => {
  let token = getCookie('token')
  let request = {
    "parent":parent,
    "deletelist":deletelist
  }
  return (dispatch) => {fetch(name._API+'/disk?action=delete&token='+token,{
      method:'POST',
      mode: 'cors',
      body: JSON.stringify(request)
  })
  .then(response => response.json())
  .then(function success(response){
    if (response.code === 200) dispatch(delete_success(response))
    else if(response.code === 403) dispatch(delete_fail(response))
  })
  }
}

export const delete_success = response => {
  return {
    type: name.DELETE_SUCCESS,
    Node: response.list,
    Dirs: response.dirs
  }
}

export const delete_fail = response => {
  return {
    type: name.DELETE_FAIL
  }
}

export const move_node = (dispatch,parent,movelist,to) => {
  let token = getCookie('token')
  let request = {
    "parent":parent,
    "movelist":movelist,
    "to":to
  }
  return (dispatch) => {fetch(name._API+'/disk?action=move&token='+token,{
      method: 'POST',
      mode: 'cors',
      body: JSON.stringify(request)
  })
  .then(response => response.json())
  .then(function success(response){
    if (response.code === 200) dispatch(move_success(response))
    else if(response.code === 403) dispatch(move_fail(response))
  })
  }
}

export const move_success = response => {
  return {
    type: name.MOVE_SUCCESS,
    Node: response.list,
    Dirs: response.dirs
  }
}

export const move_fail = response => {
  return {
    type: name.MOVE_FAIL
  }
}

export const refresh = (dispatch,parent) => {
  let token = getCookie('token')
  let request = {
    "parent":parent
  }
  return (dispatch) => {fetch(name._API+'/disk?action=list&token='+token,{
      method: 'POST',
      mode: 'cors',
      body:JSON.stringify(request)
  })
  .then(response => response.json())
  .then(function success(response){
    if (response.code === 200) {
      dispatch(refresh_success(response))
    }
  })
  }
}

export const refresh_success = response => {
  return {
    type: name.REFRESH,
    Node: response.list
  }
}

export const download = (dispatch,file) => {
  let token = getCookie('token')
  let request = {
    "file":file
  }
  return (dispatch) => {fetch(name._API+'/disk?action=download&token='+token,{
      method: 'POST',
      mode: 'cors',
      body: JSON.stringify(request)
  })
  .then(response => response.json())
  .then(function success(response){
    if (response.code === 200) {
      downloadFile('http://xhan.pw:8888/'+response.url.fdfs,response.url.ext)
      dispatch(download_success())
    }
  })
  }
}

export const download_success = () => {
  return {
    type: 'DOWNLOAD'
  }
}

